package cn.com.wblog.dao;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;

import cn.com.wblog.pojo.ArticleType;

public class ArticleTypeDao extends AbstractDao {
	
	public ArticleTypeDao(Dao dao) {
		this.dao = dao;
	}
	
	public void insert(ArticleType articleType) {
		this.dao.insert(articleType);
	}
	
	public List<ArticleType> getList(int currentPage, int numPerPage, String keyword) {
		List<ArticleType> list = this.dao.query(ArticleType.class, Cnd.where("name", "LIKE", "%"+(keyword==null?"":keyword)+"%"), dao.createPager(currentPage, numPerPage));
		for (ArticleType type : list) {
			this.dao.fetchLinks(type, "parent");
		}
		return list;
	}
	
	public ArticleType getArticleTypeById(int id) {
		ArticleType type = this.dao.fetchLinks(this.dao.fetch(ArticleType.class, id), "parent");
		return type;
	}
	
	public void update(ArticleType articleType) {
		this.dao.update(articleType);
	}
	
	public void deleteById(int id) {
		Sql sql = this.dao.sqls().create("clearParentType");
		sql.vars().set("table", "wb_article_type");
		sql.params().set("id", id);
		this.dao.execute(sql);
		sql = this.dao.sqls().create("clearTypeId");
		sql.vars().set("table", "wb_article");
		sql.params().set("id", id);
		this.dao.execute(sql);
		this.dao.delete(ArticleType.class, id);
	}
	
	public int count(String keyword) {
		return this.dao.count(ArticleType.class, Cnd.where("name", "LIKE", "%"+(keyword==null?"":keyword)+"%"));
	}
}
